package com.zhuss.enums;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.zhuss.common.EnumVo;
import com.zhuss.common.SelectVo;

public class EnumUtil {
	
	public static BaseEnum[] list(Class<? extends BaseEnum> clazz) {
		BaseEnum[] baseEnumArray = clazz.getEnumConstants();
		return baseEnumArray;
	}

	public static BaseSonEnum[] listSon(Class<? extends BaseSonEnum> clazz) {
		BaseSonEnum[] baseEnumArray = clazz.getEnumConstants();
		return baseEnumArray;
	}

	public static String returnName(Class<? extends BaseEnum> clazz, String value) {
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			if (baseEnum.sameValue(value)) {
				return baseEnum.name();
			}
		}
		return null;
	}
	
	
	public static List<SelectVo> listSelect(Class<? extends BaseEnum> clazz) {
		List<SelectVo> selectVoList = new ArrayList<SelectVo>();
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			selectVoList.add(new SelectVo(baseEnum.key(), baseEnum.value(), baseEnum.tag()));
		}
		return selectVoList;
	}
	
	public static List<EnumVo> listSelectEnum(Class<? extends BaseEnum> clazz) {
		List<EnumVo> enumVoList = new ArrayList<EnumVo>();
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			enumVoList.add(new EnumVo(baseEnum.key(), baseEnum.value(), baseEnum.tag(), baseEnum.removeIdList()));
		}
		return enumVoList;
	}
	
	public static String containsKey(Class<? extends BaseEnum> clazz, Integer key) {
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			if (baseEnum.sameKey(key)) {
				return baseEnum.value();
			}
		}
		return null;
	}

	public static List<SelectVo> containsKeyReturnSonSelect(Class<? extends BaseSonEnum> clazz, Integer key) {
		BaseEnum[] baseEnumArray = containsKeyReturnSon(clazz, key);
		if (baseEnumArray == null || baseEnumArray.length == 0) {
			return Collections.emptyList();
		}
		List<SelectVo> result = new ArrayList<>();
		for (BaseEnum baseEnum : baseEnumArray) {
			SelectVo item = new SelectVo(baseEnum.key(), baseEnum.value(), baseEnum.tag());
			result.add(item);
		}
		return result;
	}

	private static BaseEnum[] containsKeyReturnSon(Class<? extends BaseSonEnum> clazz, Integer key) {
		BaseSonEnum[] baseEnumArray = listSon(clazz);
		for (BaseSonEnum baseEnum : baseEnumArray) {
			if (baseEnum.sameKey(key)) {
				return baseEnum.sonList();
			}
		}
		return null;
	}

	public static Object containsKeyReturnData(Class<? extends BaseEnum> clazz, Integer key) {
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			if (baseEnum.sameKey(key)) {
				return baseEnum.data();
			}
		}
		return null;
	}
	
	public static Integer containsValue(Class<? extends BaseEnum> clazz, String value) {
		BaseEnum[] baseEnumArray = list(clazz);
		for (BaseEnum baseEnum : baseEnumArray) {
			if (baseEnum.value().equals(value)) {
				return baseEnum.key();
			}
		}
		return null;
	}
	
	
	public static Long containsSystem(Class<SystemEnum> clazz, Integer system) {
		SystemEnum[] systemEnumList = clazz.getEnumConstants();
		for (SystemEnum systemEnum : systemEnumList) {
			if (systemEnum.sameKey(system)) {
				return systemEnum.menuId;
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(containsSystem(SystemEnum.class, 40));
	}

}
